package com.china08.yxyapi.api.home.index.resource;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.DateUtils;

/**
 * 首页--精品资源
 * 
 */
@CrossOrigin
@RestController
public class IndexNewResourceController {

    @Autowired
    private YxyResourceService yrs;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/new/resource", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(@RequestBody RepModel reqModel) {
        String areaId = reqModel.areaId;
        // Assert.notNull(StringUtils.defaultIfBlank(areaId, null),
        // "请传区域码或刷新页面");
        List<RespModel> respModels = new ArrayList<IndexNewResourceController.RespModel>();
        if (StringUtils.isBlank(areaId)) { return respModels; }
        List<ElasticYxyResResource> list = yrs.homeIndexNewResourceTop(areaId, 16);

        List<String> userIds = new ArrayList<String>();
        for (ElasticYxyResResource elasticYxyResResource : list) {
            RespModel model = new RespModel();
            model.resId = elasticYxyResResource.getId();
            model.title = elasticYxyResResource.getTitle();
            model.schoolName = elasticYxyResResource.getUploaderSchoolName();
            model.uploadName = elasticYxyResResource.getUploaderName();
            String userId = elasticYxyResResource.getUploaderId();
            model.userId = userId;
            model.date = DateUtils.format(elasticYxyResResource.getUploadTime());
            userIds.add(userId);
            respModels.add(model);
        }
        return respModels;
    }

    protected static class RepModel extends YxyApiReqModel {
        // 区域Id码
        public String areaId;

    }

    protected static class RespModel extends YxyApiRespModel {

        public String date; // 时间 已经格式化好了

        public String resId;// 微课id

        public String title;// 标题

        public String schoolName;// 学校名称

        public String uploadName;// 上传者名称

        public String userId; // 用户Id

        public String avatar;

    }
}
